package com.baselibrary.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import com.baselibrary.R;
import com.baselibrary.utils.CountDown;
import com.baselibrary.utils.CountDownInterface;

import java.util.Random;

public class TwiceLike extends RelativeLayout {
    private Context mContext;
    float[] num = {-30, -20, 0, 20, 30};//随机心形图片角度

    private int INTERVAL = 500;
    private int WHAT = 9999;
    private long singleClickTime = 0;
    public OnDownClickListener mOnDownClickListener;
    private CountDown mCountDown;

    public TwiceLike(Context context) {
        super(context);
        initView(context);
    }

    public TwiceLike(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TwiceLike(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        mCountDown = new CountDown(WHAT, new CountDownInterface() {
            @Override
            public void countDownIng(int time) {

            }

            @Override
            public void countDownOver() {
                if (mOnDownClickListener != null)
                    mOnDownClickListener.onClickListener(false);
            }

            @Override
            public void countDownError() {

            }
        }, INTERVAL/2);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
    }

    public void setOnDownClickListener(OnDownClickListener onDownClickListener) {
        this.mOnDownClickListener = onDownClickListener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mCountDown.startCountDown(INTERVAL, INTERVAL/2);
            long newClickTime = System.currentTimeMillis();
            if (newClickTime - singleClickTime < INTERVAL) {
                mCountDown.removeMessages();
                final ImageView imageView = new ImageView(mContext);
                LayoutParams params = new LayoutParams(250, 250);
                params.leftMargin = (int) event.getX() - 150;
                params.topMargin = (int) event.getY() - 300;
                imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_zan2));
                imageView.setLayoutParams(params);
                addView(imageView);
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.play(scale(imageView, "scaleX", 2f, 0.9f, 100, 0))
                        .with(scale(imageView, "scaleY", 2f, 0.9f, 100, 0))
                        .with(rotation(imageView, 0, 0, num[new Random().nextInt(4)]))
                        .with(alpha(imageView, 0, 1, 100, 0))
                        .with(scale(imageView, "scaleX", 0.9f, 1, 50, 150))
                        .with(scale(imageView, "scaleY", 0.9f, 1, 50, 150))
                        .with(translationY(imageView, 0, -600, 800, 400))
                        .with(alpha(imageView, 1, 0, 300, 400))
                        .with(scale(imageView, "scaleX", 1, 3f, 700, 400))
                        .with(scale(imageView, "scaleY", 1, 3f, 700, 400));
                animatorSet.start();
                animatorSet.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        removeViewInLayout(imageView);
                    }
                });
                if (mOnDownClickListener != null)
                    mOnDownClickListener.onClickListener(true);
                return true;//不在传递
            }
            singleClickTime = newClickTime;
        }
        return super.onTouchEvent(event);
    }

    public static ObjectAnimator scale(View view, String propertyName, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , propertyName
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator translationX(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "translationX"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator translationY(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "translationY"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator alpha(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "alpha"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator rotation(View view, long time, long delayTime, float... values) {
        ObjectAnimator rotation = ObjectAnimator.ofFloat(view, "rotation", values);
        rotation.setDuration(time);
        rotation.setStartDelay(delayTime);
        rotation.setInterpolator(new TimeInterpolator() {
            @Override
            public float getInterpolation(float input) {
                return input;
            }
        });
        return rotation;
    }


    public interface OnDownClickListener {
        void onClickListener(boolean isLike);
    }
}